package com.wuster.controller;


import com.github.pagehelper.PageInfo;
import com.wuster.entity.OrderDetailsVO;
import com.wuster.entity.OrderListVO;
import com.wuster.entity.PageBean;
import com.wuster.exception.OrderException;
import com.wuster.result.PageResult;
import com.wuster.result.Result;
import com.wuster.result.ResultCodeEnum;
import com.wuster.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@RestController
@RequestMapping("/order")
@Api(tags="订单相关接口")
public class OrderController {

    @Autowired
    private OrderService orderService;

    //首页订单信息统计
    @GetMapping("/getOrderStatistics")
    @ApiOperation("订单信息统计")
    public Result getOrderStatistics(){
        Map<String, Integer> resultMap = orderService.orderInfo();
        log.info("获取后台订单统计信息");
        return Result.success(resultMap);
    }

    //获取订单详情
    @GetMapping("/getOrderDetails/{orderId}")
    @ApiOperation("根据id获取订单详情")
    public Result getOrderDetails(@PathVariable int orderId){
                OrderDetailsVO orderDetails = orderService.getOrderDetails(orderId);
                if(orderDetails.getId() != null){
                    log.info("根据订单id{}获取订单详情",orderId);
                    return Result.success(orderDetails);
                }else{
                    return Result.error(ResultCodeEnum.ORDERID_ERROR.getCode(), ResultCodeEnum.ORDERID_ERROR.getMessage());
                }
    }


    //获取订单列表分页查询(包含按条件查询)
    @GetMapping("getOrderList/{pageId}")
    @ApiOperation("分页查询获取订单列表")
    public Result getOrderList(@PathVariable("pageId")int pageCurrent,
                                @RequestParam(required = false) Integer state,@RequestParam(required = false)  String number,
                               @RequestParam(required = false)  String userName){

        if(pageCurrent<=0){
            return Result.error(ResultCodeEnum.ORDERLIST_PAGECURRENT_NOT_EXIST.getCode(), ResultCodeEnum.ORDERLIST_PAGECURRENT_NOT_EXIST.getMessage());
        }
//        if(pageSize==0){
//            return Result.error(ResultCodeEnum.ORDERLIST_PAGESIZE_IS_ZERO.getCode(), ResultCodeEnum.ORDERLIST_PAGESIZE_IS_ZERO.getMessage());
//        }

        //取总的订单数,然后计算总的页数,校验
        int pageSize=10;
        long count = orderService.count();
        int totalPages=0;
        if(((int)count % pageSize)!=0){
            totalPages=(int)count/pageSize+1;
        }else{
            totalPages=(int)count/pageSize;
        }
        if(totalPages<pageCurrent){
            return Result.error(ResultCodeEnum.ORDERLIST_PAGECURRENT_NOT_EXIST.getCode(),ResultCodeEnum.ORDERLIST_PAGECURRENT_NOT_EXIST.getMessage());
        }

        Map<String,Object>map=new HashMap<>();
        map.put("pageCurrent",pageCurrent);
        map.put("pageSize",pageSize);
        map.put("state",state);
        map.put("number",number);
        map.put("userName",userName);

        PageInfo<OrderListVO> orderList = orderService.getOrderList(map);
        if(orderList.getSize()==0 ){
            PageResult orderResult=new PageResult(0,pageCurrent,orderList.getList());
            return Result.success(orderResult);
        }
        PageResult orderResult= new PageResult(orderList.getTotal(),pageCurrent,orderList.getList());

        log.info("获取订单列表:{},{},{}", state,number,userName);
        return Result.success(orderResult);
    }



}
